{% template "admin/header.html" %}
<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>文件分类</legend>
    </fieldset>
    <blockquote class="layui-elem-quote">
        <a href="/admin/upload_file_type/addUI" class="layui-btn layui-btn-sm">添加</a>
    </blockquote>
    <table class="layui-table" id="list" lay-filter="list"></table>
</div>

{% template "admin/footer.html" %}
<script type="text/html" id="order">
    <input name="{{d.Id}}" data-id="{{d.Id}}" class="list_order layui-input" value="{{d.Sort}}" size="10"/>
</script>
<script type="text/html" id="action">
    {{# if(d.Id!=1&&d.Pid!=1){ }}
    <a href="/admin/upload_file_type/addUI?pid={{d.Id}}" class="layui-btn layui-btn-normal layui-btn-xs">添加</a>
    {{# } }}
    <a lay-event="edit" class="layui-btn layui-btn-xs">编辑</a>
    {{# if(d.Id!=1&&d.Pid!=1){ }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    {{# } }}
</script>


<script>
    layui.use(['table','form'], function() {
        var table = layui.table,form = layui.form, $ = layui.jquery;
        var tableIn = null;

        $.post('/admin/upload_file_type/query',function (res) {
            tableIn = table.render({
                id: 'link',
                elem: '#list',
                where:{},
                cols: [[
                    {field: 'ltitle', title: '分类名称', width: 200},
                    {field: 'sort',align: 'center',title: '排序', width: 120, templet: '#order', sort: true},
                    {width: 300, align: 'center', toolbar: '#action'}
                ]],
                data:hanMenu(res.Data,0,0)
            });
        });

        function hanMenu(data,pid,level){
            var arr = [];
            var tem = null;
            for (var i=0; i<data.length; i++){
                if(data[i].Pid == pid){
                    tem = data[i];
                    tem['lvl'] = level + 1;
                    tem['lefthtml'] = "|— ".repeat(level);
                    tem['ltitle'] = tem['lefthtml'] + tem.Title;
                    arr.push(tem);
                    arr = arr.concat(hanMenu(data,data[i].Id,tem['lvl']));
                }
            }
            return arr;
        }


        table.on('tool(list)', function(obj) {
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('您确定要删除该分类吗？', function(index){
                    var loading = layer.load(1, {shade: [0.1, '#fff']});
                    $.post("/admin/upload_file_type/delete",{id:data.Id},function(res){
                        layer.close(loading);
                        if(res.Code===1){
                            layer.msg(res.Msg,{time:1000,icon:1});
                            obj.del();
                        }else{
                            layer.msg(res.Msg,{time:1000,icon:2});
                        }
                    });
                    layer.close(index);
                });
            }else if(obj.event = 'edit'){
                layer.prompt({title:'编辑',value:data.Title},function(value, index, elem){
                    let loading =layer.load(1, {shade: [0.1,'#fff']});
                    $.post("/admin/upload_file_type/edit",{id:data.Id,title:value},function(res){
                        layer.close(loading);
                        if(res.Code===1){
                            layer.msg(res.Msg,{time:1000,icon:1},function () {
                                location.reload();
                            });
                        }else{
                            layer.msg('操作失败！',{time:1000,icon:2});
                        }
                    });
                    layer.close(index);
                });
            }
        });

        $('body').on('blur','.list_order',function() {
            var link_id = $(this).attr('data-id');
            var sort = $(this).val();
            $.post('/admin/upload_file_type/edit',{id:link_id,sort:sort},function(res){
                if(res.Code === 1){
                    layer.msg(res.Msg, {time: 1000, icon: 1});
                }else{
                    layer.msg(res.Msg,{time:1000,icon:2});
                }
            })
        });
    })
</script>
</body>
</html>